package com.hkyl.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.hkyl.hospital.entity.*;
import com.hkyl.hospital.mapper.*;
import com.hkyl.hospital.service.HospitalOperationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author sjh
 * @date 2023/07/15 10:49:16
 */
@Service
@Transactional
public class HospitalOperationServiceImpl implements HospitalOperationService {

    Logger logger = LoggerFactory.getLogger(HospitalizedServiceImpl.class);
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private HospitalizedMapper hospitalizedMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private PatientOperationMapper patientOperationMapper;
    @Autowired
    private OperationMapper operationMapper;

    @Override
    public Hospitalized selectByUID(String patientCard) {
        List<Patient> patients = patientMapper.selectByExample(null);
        List<Hospitalized> hospitalizeds = hospitalizedMapper.selectByExample(null);
        List<Patient> patientByCard = patients.stream().filter(patient -> patient.getPatientCard().equals(patientCard)).collect(Collectors.toList());

        if (patientByCard.size() == 0) {
            throw new RuntimeException("查无此人");
        }
        Integer patientId = patientByCard.get(0).getPatientId();
        for (Patient patient:patients) {
            hospitalizeds.stream().filter(hospitalized -> hospitalized.getPatientId() == patient.getPatientId()).forEach(hospitalized -> hospitalized.setPatient(patient));
        }
        List<Hospitalized> hospitalizedByName = hospitalizeds.stream().filter(hospitalized -> hospitalized.getPatientId() == patientId).collect(Collectors.toList());
        Hospitalized hospitalized = null;
        if (hospitalizedByName.size() > 0) {
            hospitalized = hospitalizedByName.get(0);
        }
        Dept dept = deptMapper.selectByPrimaryKey(hospitalized.getDeptId());
        hospitalized.setDept(dept);
        return hospitalized;
    }

    @Override
    public List<Hospitalized> selectAll() {
        return hospitalizedMapper.selectByExample(null);
    }

    @Override
    public List<PatientOperation> patientoperations(Integer pageNumber, Integer pageSize, Integer patientId) {
        PageHelper.startPage(pageNumber, pageSize);
        List<PatientOperation> patientOperations = patientOperationMapper.selectByExample(null);
        List<Operation> operations = operationMapper.selectByExample(null);
        List<Patient> patients = patientMapper.selectByExample(null);
        for (Operation operation: operations) {
            patientOperations.stream().filter(patientOperation -> patientOperation.getOperationId() == operation.getParentId()).forEach(patientOperation -> patientOperation.setOperation(operation));
        }
        for (Patient patient: patients) {
            patientOperations.stream().filter(patientOperation -> patientOperation.getPatientId() == patient.getPatientId()).forEach(patientOperation -> patientOperation.setPatient(patient));
        }
        List<PatientOperation> collect = null;
        for (Patient patient : patients) {
            collect = patientOperations.stream().filter(patientOperation -> patientOperation.getPatientId() == patientId).collect(Collectors.toList());
        }
        logger.info(patientOperations.toString()+"!!!!!!!!!!!!!!!!!");
        return collect;
    }
}
